package org.example.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import jakarta.annotation.Resource;
import org.example.annotation.Auth;
import org.example.annotation.EnableAutoRegister;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/nacos")
@EnableAutoRegister
public class NacosController {

    @NacosInjected
    private NamingService namingService;

    @Resource
    RestTemplate restTemplate;


    @GetMapping("/getInstance")
    @Auth(enable = false,token = false,level = 1,roles = {"visitor","admin"})
    public List<Instance> getNacosInstance(@RequestParam String serviceName) throws NacosException{
        return namingService.getAllInstances(serviceName);
    }

    @GetMapping("/getCloudInstance")
    @Auth(enable = false,token = false,level = 1,roles = {"visitor","admin"})
    public String getNacosCloudInstance(@RequestParam String serviceName) throws NacosException{
        return restTemplate.getForObject("http://app/echo/"+serviceName,String.class);
    }
}
